/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 石方旭
 * Date: 2022-06-23
 * Time: 8:38
 */
class Solution {
    public int lengthOfLongestSubstring(String s) {
        if (s.length() <= 1) return s.length();

        int max = 1;
        int ptr = 0;
        for (int i = 1; i< s.length(); i++) {
            // find the first occurence of the char after index ptr
            int index = s.indexOf(s.charAt(i), ptr);//字符从1下标开始第一次出现的位置
            if (index < i) { // it means that it is contained in s.substring(ptr, i)
                ptr = index + 1;
            }//是否包含当前(ptr,i)之间的子串【ptr,i)
            max = Math.max(max, i - ptr + 1);
        }

        return max;
    }
}

public class TEstDemo {

    public static void main1(String[] args) {
        String s = "helloworld";
        System.out.println(s.indexOf('w', 4));
    }

    //所有排序的复习加上二叉树昨天学习的

    public static void main(String[] args) {
         Solution solution = new Solution();
         solution.lengthOfLongestSubstring("abcabcbb");
    }

}
